package com.example.todo_list;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import org.litepal.LitePal;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;


public class add_something_todo extends AppCompatActivity {
    private static int time2 = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_something_todo);
        ActionBar ac = getSupportActionBar();
        if(ac!=null){
            ac.setTitle("Take a Note");
        }

        String temp = load();
        if(!TextUtils.isEmpty(temp)){
            time2 = Integer.parseInt(temp);
        }
        final EditText edit = (EditText)findViewById(R.id.edittext);
        Button temp_button = (Button)findViewById(R.id.confirm);
        temp_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String tempstr = edit.getText().toString();
                todolist templist = new todolist();
                templist.setIs_check(1);
                templist.setNumber(time2);
                templist.setMessage(tempstr);
                templist.save();
                ++time2;
                Intent intent1 = new Intent(add_something_todo.this,MainActivity.class);
                startActivity(intent1);
                add_something_todo.this.finish();



            }
        });
    }

    @Override
    protected void onDestroy(){
        super.onDestroy();
        String to_store = ""+time2;
        save(to_store);

    }
    public void save(String text){
        FileOutputStream out = null;
        BufferedWriter writer = null;
        try{
            out = openFileOutput("data", Context.MODE_PRIVATE);
            writer = new BufferedWriter(new OutputStreamWriter(out));
            writer.write(text);
        }
        catch (IOException e) {
            e.printStackTrace();
        }
        finally {
            try {
                if(writer != null){
                    writer.close();
                }
            }
            catch (IOException e ){
                e.printStackTrace();
            }
        }
    }

    public String load(){
        FileInputStream in = null;
        BufferedReader reader = null;
        StringBuilder content = new StringBuilder();
        try {
            in = openFileInput("data");
            reader = new BufferedReader(new InputStreamReader(in));
            String line  = "";
            while((line = reader.readLine())!=null){
                content.append(line);
            }
        }
        catch (IOException e){
            e.printStackTrace();
        }
        finally {
            if(reader!=null){
                try {
                    reader.close();
                }
                catch (IOException e){
                    e.printStackTrace();
                }
            }
        }
        return content.toString();
    }
}